#include "GameHUD.h"
#include "MenuScene.h"
#include "GlobalDefine.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
USING_NS_CC_EXT;

int waveCount;
static GameHUD* _sharedHUD;

GameHUD* GameHUD::sharedHUD(){
	//if (_sharedHUD == NULL) {
		_sharedHUD = new GameHUD();
        _sharedHUD->init();
    //}
    return  _sharedHUD;
}

GameHUD::~GameHUD()
{
	
}

bool GameHUD::init()
{
	bool bRet = false;
	do
	{
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();    

		CCMenuItemImage *pPauseItem = CCMenuItemImage::create(
                                                        "bt_pause.png",
                                                        "bt_pause_select.png",
                                                        this,
                                                        menu_selector(GameHUD::pauseGame));
       
        pPauseItem->setPosition(CCPointZero);
        
        CCMenu* menuPause = CCMenu::create(pPauseItem, NULL);
		menuPause->setPosition(ccp(winSize.width - pPauseItem->getContentSize().width/2 - 10, winSize.height - 30));
        menuPause->setTag(100);
        this->addChild(menuPause);        
        
		if(g_isSound)
		{
			CCMenuItemImage *pSoundOnItem = CCMenuItemImage::create(
															"bt_sound_on.png",
															"bt_sound_on_select.png",
															this,
															menu_selector(GameHUD::soundOn));
      
			pSoundOnItem->setPosition(CCPointZero);
    
			CCMenu* menuSoundOn = CCMenu::create(pSoundOnItem, NULL);
			menuSoundOn->setPosition(ccp(winSize.width - pPauseItem->getContentSize().width/2 - 60, winSize.height - 30));
			menuSoundOn->setTag(101);
    
			this->addChild(menuSoundOn);
		}
		else
		{
			CCMenuItemImage *pSoundOffItem = CCMenuItemImage::create(
															"bt_sound_off.png",
															"bt_sound_off_select.png",
															this,
															menu_selector(GameHUD::soundOff));
      
			pSoundOffItem->setPosition(CCPointZero);
    
			CCMenu* menuSoundOff = CCMenu::create(pSoundOffItem, NULL);
			menuSoundOff->setPosition(ccp(winSize.width - pPauseItem->getContentSize().width/2 - 60, winSize.height - 35));
			menuSoundOff->setTag(101);
    
			this->addChild(menuSoundOff);
		}
		
		CCDirector::sharedDirector()->resume();

		CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0 , true);
        
		bRet = true;
	} while (0);

	return bRet;
}

void GameHUD::pauseGame(cocos2d::CCObject* psender)
{	
    //if (getIsPause()) {
    // 
	//return;
    //}

	if(g_isGameOver)
		return;

    setIsPause(true);

	CCMenu* menu =  (CCMenu*)this->getChildByTag(100);
    
	CCMenuItemImage *pResumeItem = CCMenuItemImage::create(
                                                        "bt_play.png",
                                                        "bt_play_select.png",
                                                        this,
                                                        menu_selector(GameHUD::resumeGame));
       
    pResumeItem->setPosition(CCPointZero);
    
    CCMenu* menuResume = CCMenu::create(pResumeItem, NULL);
    menuResume->setPosition(menu->getPosition());
    menuResume->setTag(100);
    
    menu->removeFromParent();
    this->addChild(menuResume);

    CCDirector::sharedDirector()->pause();
}

void GameHUD::resumeGame(cocos2d::CCObject* psender)
{
    //if (!getIsPause()) {
    //    return;
    //}

	if(g_isGameOver)
		return;

    setIsPause(false);
    CCMenu* menu =  (CCMenu*)this->getChildByTag(100);
    
    CCMenuItemImage *pPauseItem = CCMenuItemImage::create(
                                                        "bt_pause.png",
                                                        "bt_pause_select.png",
                                                        this,
                                                        menu_selector(GameHUD::pauseGame));
       
    pPauseItem->setPosition(CCPointZero);
    
    CCMenu* menuPause = CCMenu::create(pPauseItem, NULL);
    menuPause->setPosition(menu->getPosition());
    menuPause->setTag(100);
    
    menu->removeFromParent();
    this->addChild(menuPause);

    CCDirector::sharedDirector()->resume();
}

void GameHUD::soundOn(cocos2d::CCObject* psender)
{
	if(g_isGameOver)
		return;

	if(g_isSound)
	{
		g_isSound = false;
	}

	CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
	audioEngine->pauseAllEffects();
	audioEngine->pauseBackgroundMusic();

    CCMenu* menu =  (CCMenu*)this->getChildByTag(101);
    
    CCMenuItemImage *pSoundOnItem = CCMenuItemImage::create(
                                                        "bt_sound_off.png",
                                                        "bt_sound_off_select.png",
                                                        this,
														menu_selector(GameHUD::soundOff));
       
    pSoundOnItem->setPosition(CCPointZero);
    
    CCMenu* menuSoundOff = CCMenu::create(pSoundOnItem, NULL);
    menuSoundOff->setPosition(menu->getPosition());
    menuSoundOff->setTag(101);
    
    menu->removeFromParent();
    this->addChild(menuSoundOff);
}

void GameHUD::soundOff(cocos2d::CCObject* psender)
{
	if(g_isGameOver)
		return;

	if(!g_isSound)
	{
		g_isSound = true;
	}

	CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
	audioEngine->resumeAllEffects();
	audioEngine->resumeBackgroundMusic();

    CCMenu* menu =  (CCMenu*)this->getChildByTag(101);
    
    CCMenuItemImage *pSoundOnItem = CCMenuItemImage::create(
                                                        "bt_sound_on.png",
                                                        "bt_sound_on_select.png",
                                                        this,
														menu_selector(GameHUD::soundOn));
       
    pSoundOnItem->setPosition(CCPointZero);
    
    CCMenu* menuSoundOn = CCMenu::create(pSoundOnItem, NULL);
    menuSoundOn->setPosition(menu->getPosition());
    menuSoundOn->setTag(101);
    
    menu->removeFromParent();
    this->addChild(menuSoundOn);
}
